package cc.mrbird.febs.buss.controller;

import cc.mrbird.febs.common.annotation.ControllerEndpoint;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.buss.entity.MenuTitle;
import cc.mrbird.febs.buss.service.IMenuTitleService;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.HashMap;
import java.util.Enumeration;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 *  Controller
 *
 * @author Lujingyuan
 * @date 2021-10-13 20:49:53
 */
@Slf4j
@Validated
@Controller
public class MenuTitleController extends BaseController {

    @Autowired
    private IMenuTitleService menuTitleService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "menuTitle")
    public String menuTitleIndex(){
        return FebsUtil.view("menuTitle/menuTitle");
    }

    @GetMapping("menuTitle")
    @ResponseBody
    @RequiresPermissions("menuTitle:list")
    public FebsResponse getAllMenuTitles(MenuTitle menuTitle) {
        return new FebsResponse().success().data(menuTitleService.findMenuTitles(menuTitle));
    }

    @GetMapping("menuTitle/data")
    @ResponseBody
    public FebsResponse halldepartData() {
        Map<String,Object> map=new HashMap<String,Object>();
        List<MenuTitle> list= menuTitleService.selectByMap(map);
        return new FebsResponse().success().data(list);
    }

    @GetMapping("menuTitle/list")
    @ResponseBody
    public FebsResponse menuTitleList(QueryRequest request, HttpServletRequest req, MenuTitle menuTitle) {
        Enumeration enu=req.getParameterNames();
        HashMap<String,String> map=new HashMap<String,String>();
        while(enu.hasMoreElements()){
            String paraName=(String)enu.nextElement();
            map.put(paraName,req.getParameter(paraName));
        }
        Map<String, Object> dataTable = getDataTable(this.menuTitleService.findMenuTitleDetail(request, map));
        return new FebsResponse().success().data(dataTable);
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "menuTitle/add")
    public String goMenuTitleAdd() {
        return FebsUtil.view("menuTitle/menuTitleAdd");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "menuTitle/update/{id}")
    public String goMenuTitleUpdate(@PathVariable String id, Model model) {
        MenuTitle obj= menuTitleService.getById(id);
        model.addAttribute("obj",obj);
        return FebsUtil.view("menuTitle/menuTitleUpdate");
    }

    @ControllerEndpoint(operation = "新增MenuTitle", exceptionMessage = "新增MenuTitle失败")
    @PostMapping("menuTitle")
    @ResponseBody
    public FebsResponse addMenuTitle(@Valid MenuTitle menuTitle) {
        this.menuTitleService.add(menuTitle);
        return new FebsResponse().success();
    }

    @GetMapping("menuTitle/delete/{objIds}")
    @ControllerEndpoint(operation = "删除", exceptionMessage = "删除失败")
    @ResponseBody
    public FebsResponse deleteMenuTitle( @PathVariable String objIds) {
        String[] ids = objIds.split(StringPool.COMMA);
        this.menuTitleService.deleteMenuTitle(ids);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改MenuTitle", exceptionMessage = "修改MenuTitle失败")
    @PostMapping("menuTitle/update")
    @ResponseBody
    public FebsResponse updateMenuTitle(MenuTitle menuTitle) {
        this.menuTitleService.updateMenuTitle(menuTitle);
        return new FebsResponse().success();
    }
}
